<?php

namespace App\Http\Controllers\Manage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;

use App\Model\AdCategory;

class AdCategoryController extends Controller
{
    protected static $field = [
        'id' => '编号',
        'create_time' => '创建时间',
        'update_time' => '更新时间',
        'create_user' => '创建人',
        'update_user' => '更新人',
        'rank' => '权重',
        'name' => '分类名称',
        'status' => '状态'
    ];
    /**
     * @var int 数据表id
     */
    public $table_id = 12;

    /**
     * CaseController constructor.
     */
    public function __construct()
    {
        parent::__construct($this->table_id);

    }
    //
    /**
     * 载入分类列表页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index() {
        #判断权限
        if($this->rule['view'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $obj = new AdCategory;
        $check_time = Input::get('check_time');
        $category = Input::get('category');
        $keyword = Input::get('keyword');
        $time = Input::get('create_time');
        $type = Input::get('type');
        $code = Input::get('code');
        if(Input::has('category') && Input::has('keyword')) {
            if(Input::get('category') === 'create_user' || Input::get('category') === 'update_user') {
                $user = Manage::select('id')->where('username','like',"%$keyword%")->get();
                $obj = $obj->whereIn(Input::get('category'),$user);
            }
            else {
                $obj = $obj->where(Input::get('category'),'like',"%$keyword%");
            }
        }
        if(Input::has('check_time')) {
            $start_time = date("Y-m-d 00:00:00",strtotime(substr($time , 0 , 10)));
            $end_time = date("Y-m-d 23:59:59",strtotime(substr($time,-10)));
            $obj = $obj->whereBetween('create_time',[$start_time,$end_time]);
        }
        //升降排序
        if(Input::has('type') && Input::has('code')) {
            if($code == 0) {
                $obj = $obj->orderBy($type,'asc');
            }
            if($code == 1) {
                $obj = $obj->orderBy($type,'desc');
            }
        }
        $data = $obj->paginate(10);
        $field =self::$field;
        return view('manage.ad.category.index',compact('data','field','check_time','category','keyword','time','code','type'));
    }

    /**
     * 载入添加页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create(){
        #判断权限
        if($this->rule['add'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        return view('manage.ad.category.create');
    }

    /**
     * 添加新数据
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request) {
        #判断权限
        if($this->rule['add'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $input = $request->only('name','rank','user_id');
//        dump($input);
        if(empty($input['name'])) {
            return redirect()->back()->withInput()->withErrors('不能为空!');
        }
        $some = AdCategory::where('name',$input['name'])->first();
        if($some) {
            return redirect()->back()->withInput()->withErrors('分类名已存在!');
        }
        $map = AdCategory::insert([
            'create_user' => $input['user_id'],
            'rank' => $input['rank'],
            'name' => $input['name']
        ]);
        if($map) {
            return Redirect::to('manage/ad_category');
        }
        else {
            return redirect()->back()->withInput()->withErrors('添加失败!');
        }
    }

    /**
     * 获取更新数据，载入更新页面
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id) {
        #判断权限
        if($this->rule['update'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $cate = AdCategory::where('id',$id)->first();
        return view('manage.ad.category.edit',compact('cate'));
    }

    /**
     * 更新数据
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update(Request $request,$id) {
        #判断权限
        if($this->rule['update'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $input = $request->only('user_id','rank','name');
        if(empty($input['name'])) {
            return redirect()->back()->withInput()->withErrors('名称不能为空!');
        }
        $some = AdCategory::where('name',$input['name'])->where('id','!=',$id)->first();
        if($some) {
            return redirect()->back()->withInput()->withErrors('名称重复!');
        }
        $map = AdCategory::where('id',$id)->update([
            'update_user' => $input['user_id'],
            'rank' => $input['rank'],
            'name' => $input['name']
        ]);
        if($map) {
            return Redirect::to('manage/ad_category');
        }
        else {
            return redirect()->back()->withInput()->withErrors('更新失败!');
        }
    }

    /**
     * 删除数据
     * @param $id
     * @return array
     */
    public function destroy() {
        #判断权限
        if($this->rule['delete'] == 0){
            return [
                'code' => 101,
                'msg' => '权限不足！'
            ];
        }
        $id = Input::get('id');
        $res = AdCategory::where('id',$id)->delete();
        if($res){
            return [
                'code' => 100,
                'msg' => '删除成功！'
            ];
        }else{
            return [
                'code' => 101,
                'msg' => '删除失败！'
            ];
        }
    }

    /**
     * 设置分类状态
     * @return array
     */
    public function status()
    {
        #判断权限
        if($this->rule['update'] == 0){
            return [
                'code' => 101,
                'msg' => '权限不足！'
            ];
        }
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 1) {
            $del = AdCategory::where('id',$id)->update(['status'=>1,'update_user'=>session('user.id')]);
            $msg = '启用成功！';
        }else{
            $del = AdCategory::where('id',$id)->update(['status'=>0,'update_user'=>session('user.id')]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>102,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }
}
